package payroll;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class PayrollDatabase {

    private static Map<Integer,Employee> employees = new HashMap<>();

    public static Employee getEmployee(int empId) {
        return employees.get(empId);
    }

    public static void saveEmployee(Employee e) {
        employees.put(e.getEmpId(), e);
    }
    //根据编号把它存进去，然后读取的时候 根据编号 读出来

    public static void deleteEmployee(int empId) {
        employees.remove(empId);
    }

    public static Collection<Employee> listAllEmployee() {
        return employees.values();
    }

    public static Employee[] listAllEmployees() {
        // throw new UnsupportedOperationException("Unimplemented method 'listAllEmployees'");
        
         // 获取Map中的所有键值对
        Set<Map.Entry<Integer, Employee>> entrySet = employees.entrySet();
        // 创建一个与Map大小相同的数组
        Employee[] employeeArray = new Employee[entrySet.size()];
        // 使用迭代器遍历entrySet，并填充数组
        Iterator<Map.Entry<Integer, Employee>> iterator = entrySet.iterator();
        int i = 0;
        while (iterator.hasNext()) {
            Map.Entry<Integer, Employee> entry = iterator.next();
            employeeArray[i++] = entry.getValue();
        }
        return employeeArray;
    }
}
